<html>
<head><title>Calculator</title></head>
<body>
<h1>Calculator</h1>
Type an expression into the box.
<p>

<FORM METHOD = "GET">
<INPUT TYPE = "text" NAME = "equation" VALUE = "" SIZE = 20 MAXLENGTH = 20>
<INPUT TYPE = "submit" VALUE = "Calculate">
</FORM>
<ul>
<li>This calculator will only evaluate expressions with numbers and +,-,*, and / operators.
<li>Standard order of operations applies.
<li>Parentheses are not accepted.
<li> PHP error messages are hidden.
</ul>

<?php
if($_GET["equation"]){
	$equation = $_GET["equation"];

	$nonums = preg_split('#\d#', $_REQUEST['equation'], -1, PREG_SPLIT_NO_EMPTY);

	$noops = preg_split('#[/*\-+]#', $_REQUEST['equation'], -1, PREG_SPLIT_NO_EMPTY); 
	foreach($noops as $key => $value)
		$noops[$key] = trim($value);
	if(preg_grep('#[/*\-+][/*+]#', $nonums)|| preg_grep('#[\-][\-]#', $nonums)){
		echo "Invalid Expression.". "<br/>";}
	elseif(preg_grep('#[^\.|^\d]#', $noops) || $equation[strlen($equation)-1] == '-'){
		//$wut = preg_grep('#[^\.]#', $noops);
		//echo "noops contains " . $wut[0] . " " . $wut[1] . "<br/>";
		echo "Invalid Expression." . "<br/>";}
	else{
		error_reporting(0);
		eval("\$ans = $equation;");	
		echo "Result: " . $equation . " = " .$ans . "<br />";
	}	
}

?>

</body>
</html>
